/*
 * Copyright 2005-2006, Haiku.
 * Distributed under the terms of the MIT License.
 *
 * Authors:
 *		Stefano Ceccherini (burton666@libero.it)
 */

/*!
	Pure virtual BDataIO and BPositioIO classes provide
	the protocol for Read()/Write()/Seek().

	BMallocIO and BMemoryIO classes implement the protocol,
	as does BFile in the Storage Kit.
*/

// 20060726
// R5のMallocIOはバグっているのでHaikuのものを使用
// （SetSize(0)でバッファーが空にならない。）
//
//
#include "config.h"

#ifndef __HAIKU__

#include <DataIO.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

BMallocIO::BMallocIO()
	:
	fBlockSize(256),
	fMallocSize(0),
	fLength(0),
	fData(NULL),
	fPosition(0)
{
}


BMallocIO::~BMallocIO()
{
	free(fData);
}


ssize_t
BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
{
	if (buffer == NULL)
		return B_BAD_VALUE;

	ssize_t sizeRead = 0;
	if (pos < fLength) {
		sizeRead = min_c(static_cast<off_t>(size), fLength - pos);
		memcpy(buffer, fData + pos, sizeRead);
	}
	return sizeRead;
}


ssize_t
BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
	if (buffer == NULL)
		return B_BAD_VALUE;

	size_t newSize = max_c(pos + size, static_cast<off_t>(fLength));
	status_t error = B_OK;

	if (newSize > fMallocSize)
		error = SetSize(newSize);

	if (error == B_OK) {
		memcpy(fData + pos, buffer, size);
		if (pos + size > fLength)
			fLength = pos + size;
	}
	return error != B_OK ? error : size;
}


off_t
BMallocIO::Seek(off_t position, uint32 seekMode)
{
	switch (seekMode) {
		case SEEK_SET:
			fPosition = position;
			break;
		case SEEK_END:
			fPosition = fLength + position;
			break;
		case SEEK_CUR:
			fPosition += position;
			break;
		default:
			break;
	}
	return fPosition;
}


off_t
BMallocIO::Position() const
{
	return fPosition;
}


status_t
BMallocIO::SetSize(off_t size)
{
	status_t error = B_OK;
	if (size == 0) {
		// size == 0, free the memory
		free(fData);
		fData = NULL;
		fMallocSize = 0;
	} else {
		// size != 0, see, if necessary to resize
		size_t newSize = (size + fBlockSize - 1) / fBlockSize * fBlockSize;
		if (size != fMallocSize) {
			// we need to resize
			if (char *newData = static_cast<char*>(realloc(fData, newSize))) {
				// set the new area to 0
				if (newSize > fMallocSize)
					memset(newData + fMallocSize, 0, newSize - fMallocSize);				
				fData = newData;
				fMallocSize = newSize;
			} else	// couldn't alloc the memory
				error = B_NO_MEMORY;
		}
	}

	if (error == B_OK)
		fLength = size;

	return error;
}


void
BMallocIO::SetBlockSize(size_t blockSize)
{
	if (blockSize == 0)
		blockSize = 1;
	if (blockSize != fBlockSize)
		fBlockSize = blockSize;
}


const void *
BMallocIO::Buffer() const
{
	return fData;
}


size_t
BMallocIO::BufferLength() const
{
	return fLength;
}


// Private or Reserved

BMallocIO::BMallocIO(const BMallocIO &)
{
	// copying not allowed...
}


BMallocIO &
BMallocIO::operator=(const BMallocIO &)
{
	// copying not allowed...
	return *this;
}


// FBC
void BMallocIO::_ReservedMallocIO1() {}
void BMallocIO::_ReservedMallocIO2() {}

#endif //__HAIKU__